package com.ling.module1sbbyyds.service.community;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.sbbyyds.interaction.param.CommunityTalkPo;
import com.ling.sbbyyds.entity.community.CommunityTalk;
import com.ling.sbbyyds.interaction.ResultVo;
import com.ling.sbbyyds.mapper.community.CommunityTalkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 天道酬勤、学无止境
 *
 * @Description: 社区话题
 * @author: ling
 * @date: 2022年03月27日
 */
@Service
public class CommunityTalkService {

    @Autowired(required = false)
    private CommunityTalkMapper communityTalkMapper;

    /**
     * 获取所有话题列表 （可根）
     *
     * @param page          页数
     * @param limit         条数
     * @param communityTalk 条件参数
     * @return
     */
    public ResultVo getCommunityTalkList(Integer page, Integer limit, CommunityTalkPo communityTalk) {
        Example example = new Example(CommunityTalk.class);
        //新建一个 筛选条件对象
        Example.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(communityTalk.getCommunityTalkId())) {
            //字段不为空，设置条件查询
            criteria.andEqualTo("communityTalkId", communityTalk.getCommunityTalkId());
        }
        if (!ObjectUtils.isEmpty(communityTalk.getCommunityTalkName())) {
            criteria.andLike("communityTalkName", "%" + communityTalk.getCommunityTalkName() + "%");
        }
        if (!ObjectUtils.isEmpty(communityTalk.getTalkTypeId())) {
            criteria.andEqualTo("talkTypeId", communityTalk.getTalkTypeId());
        }
        //排序
        example.setOrderByClause("talk_type_id asc, sort asc");

        //进行分页查找
        List<CommunityTalk> communityTalks;
        if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(limit)) {
            final PageInfo<CommunityTalk> pageInfo = PageHelper.startPage(page, limit)
                    .doSelectPageInfo(() -> this.communityTalkMapper.selectByExample(example));
            communityTalks = pageInfo.getList();
        } else {//不分页
            communityTalks = communityTalkMapper.selectByExample(example);
        }
        return ResultVo.success(communityTalks);
    }


}
